﻿$(function () {
    $("#submit").click(function () {
        Submit();
    });

    $("#body").keypress(function (event) {
        if (event.which == '13') {
            Submit();
        }
    });

    $(".cancelonetime").click(function () {
        if (confirm("Are you sure you want to cancel this donation?")) {
            
            var did = $(this).attr("did");
            var input = {
                DonationID: did
            };

            Ajax({
                url: "/MyAdams/Donations/CancelDonation",
                data: JSON.stringify(input),
                success: function (data) {
                    if (data.Success == true) {
                        location.href = "/MyAdams/Donations?msg=1";
                    } else {
                        location.href = "/MyAdams/Donations?msg=2";
                    }
                }
            });
        }
    });

    $(".cancelrecurring").click(function () {
        if (confirm("Are you sure you want to cancel this recurring donation?")) {

            var did = $(this).attr("did");
            var input = {
                DonationID: did
            };

            Ajax({
                url: "/MyAdams/Donations/CancelRecurringDonation",
                data: JSON.stringify(input),
                success: function (data) {
                    if (data.Success == true) {
                        location.href = "/MyAdams/Donations?msg=1";
                    } else {
                        location.href = "/MyAdams/Donations?msg=2";
                    }
                }
            });
        }
    });
});

function Submit() {

    $("#submit").hide();

    var input = {
        BucketID: $("#BucketID").val(),
        Amount: $("#Amount").val(),
        Recurring: $("#Recurring").is(":checked")
    };

    Ajax({
        url: "/MyAdams/Donations/SaveDonation",
        data: JSON.stringify(input),
        success: function (data) {
            if (data.IsValid == false) {
                ShowValidationErrors(data);
            } else if (data.IsSaved == true) {
                var recurring = input.Recurring ? "1" : "0";
                location.href = "/MyAdams/Donations/Confirm?d=" + data.ID + "&r=" + recurring;
            }
        },
        complete: function () {
            $("#submit").show();
        }
    });
}